DAY5:Delete occurrences of an element if it occurs more than n times


Posted by birdbirdmurmur on 2023-07-18

題目連結:

Delete occurrences of an element if it occurs more than n times

解題思維:

  1. 要有一個儲存數字出現次數的「物件」
  2. 對arr遍歷篩選
  3. 給物件一個屬性
  4. 數字重複就+1 沒有就從1開始
  5. 直到n回傳

實作解法:

function deleteNth(arr,n){
  let count = {};
   return arr.filter( num =>{
     count[num] = (count[num] || 0) +1
     return count[num] <= n
})
}

心得

這題真的超難!
爬完discourse只稍微整理出一點邏輯
我想我目前的程度還不足以挑戰6kyu等級......
連續兩天在6kyu花了太多時間
接下來還是先回頭到7kyu好了


#javascript #Codewars #filter #object







Related Posts

自定義 Python 模組

自定義 Python 模組

Go起手式之三

Go起手式之三

用 Python 自學資料科學與機器學習入門實戰:Pandas 基礎入門

用 Python 自學資料科學與機器學習入門實戰:Pandas 基礎入門


Comments